<?php
$options = getopt('', array("tplDir::", "dev::", "cacheDir::", "header::", "footer::"));

$cacheDir = $options["cacheDir"];
$tplDir = $options["tplDir"];
$debug = $options["dev"] ? true : false;
$startFlag = $options["header"] ? $options["header"] : "[[go_php_piper_start]]";
$endFlag = $options["footer"] ? $options["footer"] : "[[go_php_piper_finish]]";

require_once __DIR__ . "/vendor/autoload.php";
$loader = new \Twig\Loader\FilesystemLoader($tplDir);
if ($debug) {
    $opt = array("debug" => true, "auto_reload" => true, "cache" => false);
} else {
    $opt = array("debug" => false, "auto_reload" => false, "cache" => $cacheDir);
}
$twig = new Twig_Environment($loader, $opt);
if ($debug) {
    $twig->addExtension(new Twig_Extension_Debug());
}
$capitalize = new Twig_SimpleFilter("capitalize", function ($str) {
    return ucfirst($str);
});
$twig->addFilter($capitalize);
$ucfirst = new Twig_SimpleFilter("ucfirst", function ($str) {
    return ucfirst($str);
});
$twig->addFilter($ucfirst);
$lcfirst = new Twig_SimpleFilter("lcfirst", function ($str) {
    return lcfirst($str);
});
$twig->addFilter($lcfirst);

function SnakeToLowerCamel($value){
    $value = ucwords(str_replace(['_', '-'], ' ', $value));
    $value = str_replace(' ', '', $value);
    return lcfirst($value);
}
$camel = new Twig_SimpleFilter("camel", function ($str) {
    return SnakeToLowerCamel($str);
});
$twig->addFilter($camel);

function CamelToSnake($value){
    $value = preg_replace('/\s+/u', '', $value);
    $value = strtolower(preg_replace('/(.)(?=[A-Z])/u', "$1_", $value));
    return $value;
}
$snake = new Twig_SimpleFilter("snake", function ($str) {
    return CamelToSnake($str);
});
$twig->addFilter($snake);

function SnakeToUpperCamel($value){
    $value = ucwords(str_replace(['_', '-'], ' ', $value));
    $value = str_replace(' ', '', $value);
    return $value;
}

$input = "";
while ($stdin = fread(STDIN, 1024)) {
    if (substr($stdin, 0, strlen($startFlag)) == $startFlag) {
        $input = "";
    }
//    file_put_contents("./text.txt","\n",FILE_APPEND);
//    file_put_contents("./text.txt","step 1",FILE_APPEND);
//    file_put_contents("./text.txt","\n",FILE_APPEND);
//    file_put_contents("./text.txt","got",FILE_APPEND);
//    file_put_contents("./text.txt","\n",FILE_APPEND);
//    file_put_contents("./text.txt",$stdin,FILE_APPEND);
//    file_put_contents("./text.txt","\n",FILE_APPEND);
    $input .= $stdin;
//    file_put_contents("./text.txt","total",FILE_APPEND);
//    file_put_contents("./text.txt","\n",FILE_APPEND);
//    file_put_contents("./text.txt",$input,FILE_APPEND);
//    file_put_contents("./text.txt","\n",FILE_APPEND);
//    file_put_contents("./text.txt",substr(rtrim($input), strlen($endFlag) * -1),FILE_APPEND);
//    file_put_contents("./text.txt","\n",FILE_APPEND);
//    file_put_contents("./text.txt",$endFlag,FILE_APPEND);
    if (substr(rtrim($input), strlen($endFlag) * -1) != $endFlag) {
        continue;
    }
//    file_put_contents("./text.txt","\n",FILE_APPEND);
//    file_put_contents("./text.txt","step 2",FILE_APPEND);
//    file_put_contents("./text.txt","\n",FILE_APPEND);
//    $len = strlen($input);
    $input = rtrim($input);
    //转为php数组
    try {
        $conf = json_decode(substr($input, strlen($startFlag), strlen($endFlag) * -1), true);
    } catch (Exception $exception) {
        fwrite(STDOUT, $startFlag . $exception->getMessage() . $endFlag . "\n");
        $input = "";
        continue;
    }
    $template = $twig->load($conf["__tplPath"]);
    $content = $template->render($conf["data"]);
    //STDOUT 返回渲染后的数据
    $re = $startFlag . $content . $endFlag . "\n";
//    file_put_contents("./text.txt","step 3",FILE_APPEND);
    fwrite(STDOUT, $re);
}
